package calculatorPaxFileReader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import actions.MenuActions;
import calculatorAircraftFileReader.AircraftFileVariables;
import calculatorEngine.PassengerStanding;
import calculatorEngine.Seat;



public class PaxFileVariables {
	int numberOfLines;
	String readLines[];
	public String paxFilePath;
	int paxNumber;
	
	List<PassengerStanding> paxList = new ArrayList<PassengerStanding>();
	
	// zmienne potrzebne tylko i wylacznie do ostatniej metody
	String firstName;
	String lastName;
	String possibleComfortClass;
	String preferences;
	String paxSeat;
	String ticketNumber;
	String rowsInt;
	String columnsString;
	
	
	
	
	public PaxFileVariables(String path) {
		this.paxFilePath = path;
		try {
			ReadPaxFile file = new ReadPaxFile(paxFilePath);
			readLines = file.OpenFile();
			numberOfLines = file.readLines();
		} catch (IOException m) {
			System.out.println(m.getMessage());
		}

	}
	// nazwa samolotu
	public String paxName(){
		String name = readLines[2];
		return name;
	}
	
	// numer lotu
	public String paxFlightNumber(){
		String flightNumber = readLines[0];
		return flightNumber;
	}
	
	// Data lotu
	public String FlightDate(){
		String flightDate = readLines[1];
		return flightDate;
	}
	
	public int numberOfPax(){
		String numberOfPax = readLines[3];
		paxNumber = Integer.parseInt(numberOfPax);
		return paxNumber;
	}
	
	public List<Seat> assignPaxToSeat(String acft){
		int x = 3;
		String wholeShit;
		
		AircraftFileVariables lol = new AircraftFileVariables(acft);
		List<Seat> thisListMadeMeCry = lol.aircraftTestList();
		for (int i=0; i < numberOfLines - 4; i++){
			// wholeShit to cala linijka dotyczaca pasazera
			x++;
			wholeShit = readLines[x];
			// teraz ja pociacham na mniejsze zmienne
			
			// numer biletu
			ticketNumber = wholeShit.substring(0, 14);
			
			//teraz musze sie dowiedziec jak dlugie jest imie
			String space = " ";
			int h = 15;
			while (wholeShit.substring(h, h+1).equals(space) == false) {
				h++;
			}
			
			//imie pasazera
			lastName = wholeShit.substring(15, h);
			
			//teraz musze sie dowiedziec jak dlugie jest nazwisko
			int j = h+1;
			while(wholeShit.substring(j, j+1).equals(space) == false) {
				j++;
			}
			
			//nazwisko pasazera
			firstName = wholeShit.substring(h+1, j);
			
			// mozliwe klasy podrozy
			possibleComfortClass = wholeShit.substring(j+1, j+2);
			
			// preferencje dotyczace miejsce
			preferences = wholeShit.substring(j+3, j+4);
			
			// niewazne
			rowsInt = wholeShit.substring(j+5, j+7);
			
			//niewazne vol2
			columnsString = wholeShit.substring(j+7, j+8);
			
			// siedzenie
			paxSeat = wholeShit.substring(j+5, j+8);
			
			//tester
			/* System.out.println(ticketNumber 
					+space 
					+firstName 
					+space 
					+lastName 
					+space 
					+possibleComfortClass 
					+space 
					+preferences 
					+space 
					+paxSeat
					+space
					+rowsInt
					+space
					+columnsString);*/
			 
			
		
			
			int numberOfARow = Integer.parseInt(rowsInt);
			int o =( (lol.aircraftColumnsNumber()* (numberOfARow-1)) + lol.aircraftCompare(columnsString)) -1;
			//System.out.println( o);
			
			
			if (lol.aircraftCompare(columnsString) == 0){
				paxList.add(new PassengerStanding(this.firstName, this.lastName, this.possibleComfortClass, this.preferences, this.paxSeat, this.ticketNumber, this.rowsInt, this.columnsString));
			}
			else {
				
				
				thisListMadeMeCry.get(o).setStatus(1);
				thisListMadeMeCry.get(o).addFirstName(this.firstName);
				thisListMadeMeCry.get(o).addLastName(this.lastName);
				thisListMadeMeCry.get(o).setTicketNumber(this.ticketNumber);
				//if(thisListMadeMeCry.get(o).getPreferences().equals(this.preferences) == true){
					thisListMadeMeCry.get(o).setHappiness(Seat.HAPPY);
				//}else {	thisListMadeMeCry.get(o).setHappiness(Seat.NOT_HAPPY);}
			}
		
			
		}
		
		return thisListMadeMeCry;
	}
	
	public List<PassengerStanding> getPaxList(){
		return paxList;
	}
	
	public ArrayList<Seat[]> paxListOfArrays(List<Seat> paxList, String acft){
		AircraftFileVariables kupa = new AircraftFileVariables(acft);
		ArrayList<Seat[]> paxListOfArrays = new ArrayList<Seat[]>();
		int i = 0;
		int l = kupa.aircraftColumnsNumber();
		int k = kupa.aircraftRowsNumber();
		for(int a = 0; a < k; a++){
			Seat[] seats = new Seat[l];
			for(int b = 0; b < l; b++){
				seats[b] = paxList.get(i);
				i++;
				}
			paxListOfArrays.add(seats);
			}
		
		
		return paxListOfArrays;
	}
	
}
